package com.shijie.newvideo.utils;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

/**
 * 根据设置的宽高比，通过宽度自动设置高度的view，
 * <br/>继承了imageView，直接继承view不方便使用
 * <br/>只设置了view的宽高，要正常显示图片请配合{@linkplain ScaleType#FIT_XY}
 * <br/>
 * 使用方法：
 * <br/>①指定wh_rate，则以该值为宽高比
 * <br/>②如果没有指定 wh_rate（如除不尽等情况），则读取wh_width，wh_height
 * <br/>③如果 wh_width 不为0，才读取 wh_height ，且其默认值为1
 *
 * @author 刘汝寿
 * @date 2017/4/11.
 */
public class AutoResizeHeightImageView extends androidx.appcompat.widget.AppCompatImageView {
    private AutoResizeHeightHelper mAutoResizeHeightHelper;

    public AutoResizeHeightImageView(Context context) {
        super(context);
        initView(context, null, 0);
    }

    public AutoResizeHeightImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs, 0);
    }

    public AutoResizeHeightImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        mAutoResizeHeightHelper = new AutoResizeHeightHelper(context, attrs, defStyleAttr);
    }

    public AutoResizeHeightHelper getAutoResizeHeightHelper() {
        return mAutoResizeHeightHelper;
    }

    public void setWhRate(float whRate) {
        if (mAutoResizeHeightHelper != null) {
            mAutoResizeHeightHelper.setWhRate(whRate);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //先交给父类计算宽高
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mAutoResizeHeightHelper.getWhRate() != 0) {
            int w = getMeasuredWidth();
            int h = ViewUtils.getAutoResizeHeight(this, w, mAutoResizeHeightHelper.getWhRate());
            //view
            setMeasuredDimension(w, h);
            //view group 高度mode设置为EXACTLY
            //super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.getMode(widthMeasureSpec)), MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY));
        }
        /*尝试处理view group的写法
        if (mAutoResizeHeightHelper.getWhRate() != 0) {
            int w = getMeasuredWidth();
            int h = ViewUtils.getAutoResizeHeight(this, w, mAutoResizeHeightHelper.getWhRate());
            setMeasuredDimension(w, h);
            if (this instanceof ViewGroup) {
                for (int index = 0; index < getChildCount(); index++) {
                    View child = getChildAt(index);
                    child.measure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(h, MeasureSpec.AT_MOST));
                }
            }
        }
        */
        /* 无法测量view group子view的写法
        if (mAutoResizeHeightHelper.getWhRate() != 0) {
            int w = getMeasuredWidth();
            int h = ViewUtils.getAutoResizeHeight(this, w, mAutoResizeHeightHelper.getWhRate());
            setMeasuredDimension(w, h);
        }
        */
        /*稍早的写法
        if (mWhRate != 0) {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            if (widthMode == MeasureSpec.EXACTLY) {
                int w = MeasureSpec.getSize(widthMeasureSpec);
                int h = ViewUtils.getAutoResizeHeight(this, w, mWhRate);
                setMeasuredDimension(w, h);
            } else {
                //没有指定exactly，交给父类计算宽高
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                int w=getMeasuredWidth();
                int h = ViewUtils.getAutoResizeHeight(this, w, mWhRate);
                setMeasuredDimension(w, h);
            }
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
         */
    }
}
